<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48" >
            <a-col :md="8" :sm="24">
              <a-form-item label="批次名称">
                <a-input v-model="queryParam.name" placeholder="请输入批次名称"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="批次号">
                <a-input v-model="queryParam.number" placeholder="请输入批次号"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md=" 8 || 24" :sm="24">
              <a-form-item>
                <a-button type="primary" @click="queryTable">查询</a-button>
              </a-form-item>
            </a-col>
            <a-col :md=" 8 || 24" :sm="24">
              <a-form-item label="商品名称">
                <a-input v-model="queryParam.goods_name" placeholder="请输入上名称"></a-input>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table :columns="columns" :dataSource="dataList" :pagination="false" rowKey="id">
        <a slot="name" slot-scope="text" href="javascript:">{{ text }}</a>
        <span slot="action" slot-scope="record">
          <a href="javascript:" @click.prevent="batchEdit(record)">编辑</a>
          <a-divider type="vertical"></a-divider>
          <a-popconfirm
            v-if="dataList.length"
            title="确认下载二维码?"
            @confirm="() => getQR(record)">
            <a href="javascript:">下载二维码</a>
          </a-popconfirm>
          <!--<a href="javascript:" @click.prevent="getQR(record.qr_code_url)">下载二维码</a>-->
        </span>
      </a-table>
      <my-pagination
        :total="pagination.totalCount"
        :pageSize="pagination.pageSize"
        :current="pagination.pageNo"
        @onChange="onChangePage"
        @onShowSizeChange="onShowSizeChangePage"
      ></my-pagination>
    </a-card>
  </div>
</template>

<script>
import MyPagination from '../../components/Autism/myPagination'
import { queryBatchPage } from '@/api/batch'

const columns = [
  {
    title: '批次号',
    dataIndex: 'number',
    key: 'number'
  },
  {
    title: '批次名称',
    dataIndex: 'name',
    key: 'name'
  }, {
    title: '产品名称',
    dataIndex: 'goods_name',
    key: 'goods_name'
  }, {
    title: '创建时间',
    key: 'create_date_time',
    dataIndex: 'create_date_time'
  }, {
    title: '操作',
    key: 'action',
    scopedSlots: { customRender: 'action' }
  }]
export default {
  name: 'BatchList',
  components: {
    MyPagination
  },
  data: function () {
    return {
      pagination: {
        pageNo: 1,
        pageSize: 10,
        totalCount: 0
      },
      currentTab: 0,
      queryParam: {},
      columns,
      dataList: []
    }
  },
  filter: {},
  mounted () {
    this.getList()
  },
  methods: {
    // 编辑批次
    batchEdit (batch) {
      this.$router.push({ path: 'edit-batch', query: batch })
    },
    // 获取二维码
    getQR (record) {
      if (record.qr_code_url) {
        // window.location.href = 'https://admin.muyumall.com' + record.qr_code_url
        window.open('https://admin.muyumall.com/' + record.qr_code_url, '_blank')
      } else {
        this.$message.warning('二维码URL为空！')
      }
      /* getBatchQR({
        domain: record.qr_code_url
      })
        .then((res) => {
          console.log(res)
          if (res) { this.$message.success('成功！') }
        })
        .catch((err) => {
          console.log(err)
        }) */
    },
    // 分页获取列表
    getList () {
      queryBatchPage({
        page_index: this.pageNo,
        page_size: this.pageSize
      })
        .then((res) => {
          this.dataList = res.collection
          this.pagination.pageNo = res.page_index
          this.pagination.pageSize = res.page_size
          this.pagination.totalCount = res.total_count
        })
        .catch((err) => {
          console.log(err)
        })
    },
    queryTable () {
      const param = {
        page_index: 1,
        page_size: 10
      }
      queryBatchPage(Object.assign(this.queryParam, param))
        .then((res) => {
          this.dataList = res.collection
          this.pagination.pageNo = res.page_index
          this.pagination.pageSize = res.page_size
          this.pagination.totalCount = res.total_coun
        })
        .catch((err) => {
          console.log(err)
        })
    },
    onChangePage (pageNo, pageSize) {
      this.pagination.pageNo = pageNo
      // 重新请求数据
      this.getList()
    },
    onShowSizeChangePage (pageNo, pageSize) {
      this.pagination.pageSize = pageSize
      // 重新请求数据
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
